<template>
	<view>
		<view class="u-p-40 bg-white">
			<text class="u-font-30 font-weight-bold">充值金额</text>
			<view class="flex u-m-t-20">
				<text class="u-m-r-30 u-font-40 u-m-t-6">¥</text>
				<u-input v-model="number" type="digit" placeholder="输入充值金额"></u-input>
			</view>
		</view>
		<view class="flex flex-column u-m-30 u-p-30 bg-white rounded-10 box-shadow ">
			<text>请选择充值方式</text>
			<view class="flex justify-between" @click="itemClick(index)" v-for="(item, index) in list" :key="index" style="width: 100%;">
				<view class="flex align-center u-p-y-20" >
					<view class="flex">
						<u-icon v-if="index == 0" name="zhifubao-circle-fill" size="50" color="#06b4fd"></u-icon>
						<u-icon v-if="index == 1" name="weixin-fill" size="50" color="#00c800"></u-icon>
					</view>
					<text class="u-m-l-20 u-font-26">{{item}}</text>
				</view>
				<u-radio-group v-model="value" :activeColor="'var(--mainColor)'">
					<u-radio :name="index"></u-radio>
				</u-radio-group>
			</view>
			<view class="">
				<text class="u-font-24">备注(选填)</text>
				<view class="u-m-t-20 u-p-x-20 rounded-10" style="background-color: #f6f4f8;">
					<u-input v-model="remark" placeholder="输入备注" type="textarea"></u-input>
				</view>
			</view>
		</view>
		<view class="" style="margin: 80rpx 100rpx;">
      <dy-button size="default" width="576rpx" @click="sure" :isLoading="isLoading">提交申请</dy-button>
		</view>
		<web-view v-if="pay_url" :src="pay_url"></web-view>
	</view>
</template>

<script>
	import { BaseCenterApi } from '@/api/serves.js'
	// #ifdef MP-WEIXIN
	//  const DGBillPlugin = requirePlugin('dg-bill');
	// #endif
	export default {
		data() {
			return {
				value: 1,
				list: ['支付宝支付', '微信支付'],
				number: '',
				remark: '',
				isLoading: false,
				pay_url:''
			};
		},
		methods: {
			itemClick(index) {
				if(this.value == index) return
				this.value = index
			},
			sure() {
				if(!this.$u.test.amount(this.number)) return this.$u.toast('输入正确的金额')
				// if(!this.value) return this.$u.toast('暂未开放支付宝支付')
				let pay_type = this.value == 1?'wechat':'alipay'
				this.isLoading = true
				uni.showLoading({	title:'支付中...'})
				BaseCenterApi.do_recharge({number: this.number, remark: this.remark, pay_type}).then(res=> {
					console.log('充值', res);
					if(res.code == 1){
						this._huifuPay(res.data)
					}else {
						this.isLoading = false
						uni.hideLoading()
						this.$u.toast(res.msg)
					}
				})
			},
			paySuccess(){
				console.log('成功隐藏');
				this.isLoading = false
				uni.hideLoading()
				if(this.pre_order_id){
					uni.redirectTo({
						url:'/pages/goodsDetail/goodsOrderPayComplate/goodsOrderPayComplate?type=1'
					})
				}else{
					uni.redirectTo({
						url:'/pages/order/mineOrderList/mineOrderList?type=2'
					})
				}
				console.log('跳转到待发货页面');
			},
			payError(){
				console.log('失败隐藏');
				this.isLoading = false
				uni.hideLoading()
				if(this.pre_order_id){
					uni.redirectTo({
						url:'/pages/goodsDetail/goodsOrderPayComplate/goodsOrderPayComplate?type=0'
					})
				}
			},
			aliPay(params){
				// 跳转支付宝支付
				BaseCenterApi.huifuPay(params).then(res=>{
					console.log(res);
					this.isLoading = false
					uni.hideLoading()
					if (res.code == 1) {
						let data = res.data.qr_code  // 莫名其妙，这行注释掉就跳不过去了
						console.log('跳转支付宝支付',data);
						this.pay_url = res.data.qr_code
					}else{
						this.$u.toast(res.msg)
					}
				})
			},
			_huifuPay(data) {
				let params = {
					order_no: data.order_no,
					pay_type: this.value == 1?'wechat':'alipay',
					order_type: 20,
					// #ifdef APP-PLUS
					pay_method: "app"
					// #endif
					// #ifdef MP-WEIXIN
					pay_method: "miniapp"
					// #endif
					
				}
				console.log(params);
				// #ifdef MP-WEIXIN
				   console.log(params);
					if(this.value == 1){ //使用斗拱插件调支付页面
						BaseCenterApi.huifuWxPay(params).then(res => {
							// DGBillPlugin.requestPay(res.data.pre_order_id,this.paySuccess,this.payError )
							// wx.navigateToMiniProgram({
							//   appId: 'wx11361ccf7f47b948',//固定填写斗拱账单小程序
							//   path: '/pages/cashier/cashier?p=' + res.data.pre_order_id
							// })
							wx.requestPayment({
								...res.data,
								"prepayid": res.data.package.split('=')[1], // 统一下单订单号
								"sign": "MD5", // 签名，这里用的 MD5/RSA 签名
								success:this.paySuccess,
								fail:(err)=>{
									console.log(err)
								}
							})
						})
					}else{
						this.isLoading = false 
						this.$u.toast('暂不支持支付宝支付')
						return
						this.aliPay(params)
					}
				// #endif
				
				// #ifdef APP-PLUS
					if(this.value == 1){
						//跳转小程序支付
						plus.share.getServices(res => {
							this.sweixin = res.find(i => i.id === 'weixin')
							if (this.sweixin) {
								// 分享跳转到微信小程序
								BaseCenterApi.huifuWxPay(params).then(res => {
									uni.hideLoading()
									params.pre_order_id = res.data.pre_order_id
									this.sweixin.launchMiniProgram({
										id: 'gh_7332f30f32d0', // 	关联微信小程序的原始ID（"g_"开头的字符串）
										path:  "pages/goodsDetail/goodsOrderPay/goodsOrderPay?orderInfo=" + JSON.stringify(params), //	打开小程序的页面路径，不传默认跳转首页
										type: 0 // 	微信小程序版本类型，可取值： 0-正式版； 1-测试版； 2-体验版。 默认值为0。
									}, res => {
										this.isLoading = false
										console.log('成功唤起微信小程序'); 
									}, err => {
										console.log('失败');
									})
								})
							} else {
								// 没有获取到微信分享服务
								console.log('没有获取到微信分享服务');
							}
						}, err => {
							// 获取分享服务列表失败
							console.log('获取分享服务列表失败');
						});
					}else{
						this.aliPay(params)
					}
					
				// #endif
			}
		}
	}
</script>

<style lang="scss">

</style>
